package com.sky.controller.admin;

import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "分类管理相关接口")
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /*
     * 条件分页查询
     * */
    @GetMapping("/page")
    @ApiOperation("条件分页查询")
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO) {
        log.info("条件分页查询:{}", categoryPageQueryDTO);
        PageResult page = categoryService.page(categoryPageQueryDTO);
        return Result.success(page);
    }

    /*
     * 新增分类
     * */
    @PostMapping
    @ApiOperation("新增分类")
    public Result save(@RequestBody Category category) {
        log.info("新增分类:{}", category);
        categoryService.add(category);
        return Result.success();
    }

    /*
     * 修改分类
     * */
    @ApiOperation("修改分类")
    @PutMapping
    public Result update(@RequestBody Category category) {
        log.info("修改分类:{}", category);
        categoryService.update(category);
        return Result.success();
    }

    /*
     * 启用禁用
     * */
    @ApiOperation("启用禁用分类")
    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable Integer status, @RequestParam Long id) {
        log.info(status == 1 ? "启用" : "禁用" + "分类:{}", id);
        categoryService.enableOrDisable(id, status);
        return Result.success();
    }

    /*
     * 删除分类
     * */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result delete(@RequestParam Long id) {
        log.info("删除分类:{}", id);
        categoryService.delete(id);
        return Result.success();
    }

    /*
     * 根据类型查询分类
     * */
    @GetMapping("/list")
    public Result<List<Category>> getById(@RequestParam Integer type) {
        log.info("根据类型查询分类:{}", type);
        List<Category> categorys = categoryService.getByType(type);
        return Result.success(categorys);
    }


}
